package org.zk.controller;

import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.zk.entity.Result;
import org.zk.entity.User;
import org.zk.service.UserService;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/test1")
    public String test1() {
        return "testttttt";
    }


    @RequestMapping("/getUser")
    public User getUser() {
        User user = new User();
        user.setId(1);
        user.setUserName("admin");
        user.setPassWord("123");
        return user;
    }

    @RequestMapping("/toIndex")
    public ModelAndView toIndex() {
        return new ModelAndView("index");
    }


    @RequestMapping("/getPage")
    public Map queryUsersByPage(int pageSize, int pageNum, User user) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        map.put("user", user);

        Page page = userService.queryLikeUsers(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());
        return map;
    }


    @RequestMapping("/editPage")
    public ModelAndView editPage(User user) {
        User u = userService.getUserById(user.getId());
        Map<String, Object> maps = new HashMap<>();
        maps.put("data", u);
        return new ModelAndView("edit", maps);
    }


    @RequestMapping("/edit")
    public Result editUser(User user) {
        userService.updateUser(user);
        return new Result();
    }


    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
